import storage from 'store'
import { login, logout } from '@/services/user'
import { ACCESS_TOKEN, ACCESS_REFRESH_TOKEN, USER_NAME, USER_INFO, PERMISSIONS, VUE_APP_ROLES, UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'
export default {
  namespaced: true,
  state: {
    user: undefined,
    permissions: null,
    roles: null,
    routesConfig: null,
    token: null,
    refreshToken: null
  },
  getters: {
    user: state => {
      if (!state.user) {
        try {
          const user = storage.get(USER_INFO)
          state.user = user
          state.user = state.user ? state.user : {}
        } catch (e) {
          console.error(e)
        }
      }
      return state.user
    },
    permissions: state => {
      if (!state.permissions) {
        try {
          const permissions = storage.get(PERMISSIONS)
          state.permissions = permissions
          state.permissions = state.permissions ? state.permissions : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.permissions
    },
    roles: state => {
      if (!state.roles) {
        try {
          // const roles = storage.get(VUE_APP_ROLES)
          // state.roles = roles
          state.roles = state.roles ? state.roles : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.roles
    },
    routesConfig: state => {
      if (!state.routesConfig) {
        try {
          // const routesConfig = storage.get(VUE_APP_ROLES)
          // state.routesConfig = routesConfig
          state.routesConfig = state.routesConfig ? state.routesConfig : []
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.routesConfig
    },
    token: state => {
      try {
        const token = storage.get(ACCESS_TOKEN)
        state.token = token
      } catch (e) {
        console.error(e.message)
      }
      return state.token
    },
    refreshToken: state => {
      if (!state.refreshToken) {
        try {
          const refreshToken = storage.get(ACCESS_REFRESH_TOKEN)
          state.refreshToken = refreshToken
        } catch (e) {
          console.error(e.message)
        }
      }
      return state.refreshToken
    }
  },
  mutations: {
    setUser(state, user) {
      state.user = user
      storage.set(USER_INFO, user)
      storage.set(USER_NAME, user.username)
    },
    setPermissions(state, permissions) {
      state.permissions = permissions
      storage.set(PERMISSIONS, permissions)
    },
    setRoles(state, roles) {
      state.roles = roles
      // storage.set(VUE_APP_ROLES, roles)
    },
    setRoutesConfig(state, routesConfig) {
      state.routesConfig = routesConfig
      // storage.set(VUE_APP_ROLES, routesConfig)
    },
    setToken: (state, token) => {
      state.token = token
      // storage.set(ACCESS_TOKEN, token, new Date().getTime() + 10 * 1000)
      storage.set(ACCESS_TOKEN, token, new Date().getTime() + 5 * 60 * 1000)
    },
    setRefreshToken: (state, token) => {
      state.refreshToken = token
      storage.set(ACCESS_REFRESH_TOKEN, token)
    },
    clearStorage: (state) => {
      state.user = null
      state.permissions = null
      state.routesConfig = null
      state.token = null
      state.refreshToken = null
      storage.remove(ACCESS_TOKEN)
      storage.remove(ACCESS_REFRESH_TOKEN)
      storage.remove(VUE_APP_ROLES)
      storage.remove(PERMISSIONS)
      storage.remove(VUE_APP_ROLES)
      storage.remove(UI_CACHE_DB_DICT_DATA)
    }
  },
  actions: {
    // 登录
    Login({ commit }, data) {
      return new Promise((resolve, reject) => {
        login(data).then(response => {
          if (response.code === 200) {
            const result = response.result
            const { user, permissions, roles, token, refreshToken } = result
            commit('setUser', user)
            commit('setPermissions', permissions)
            commit('setRoles', roles)
            commit('setToken', token)
            commit('setRefreshToken', refreshToken)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({ commit }) {
      return new Promise((resolve) => {
        commit('clearStorage')
        logout(storage.get(USER_NAME)).then(() => {
          resolve()
        }).catch((err) => {
          console.log('logout fail:', err)
          resolve()
        })
      })
    }
  }
}
